package com.googlecode.npgame.graphics.shaders;

import android.opengl.GLES20;

/**
 * @author Josh (joshbruning)
 */
public class Program {

  private int program;
  private int vertexHandle;
  private int projectionMatrixHandle;
  private int modelViewMatrixHandle;

  public Program(Shader vertexShader, Shader fragmentShader) {
    this.program = GLES20.glCreateProgram();
    GLES20.glAttachShader(this.program, vertexShader.toInt());
    GLES20.glAttachShader(this.program, fragmentShader.toInt());
    GLES20.glLinkProgram(this.program);

    this.vertexHandle = GLES20.glGetAttribLocation(this.program, "Vertex");
    this.projectionMatrixHandle = GLES20.glGetUniformLocation(this.program,
        "ProjectionMatrix");
    this.modelViewMatrixHandle = GLES20.glGetUniformLocation(this.program,
        "ModelViewMatrix");
  }

  public int toInt() {
    return this.program;
  }

  public void setProjectionMatrix(float[] projMatrix) {
    GLES20.glUniformMatrix4fv(projectionMatrixHandle, 1, false, projMatrix, 0);
  }

  public void setModelViewMatrix(float[] modelViewMatrix) {
    GLES20.glUniformMatrix4fv(modelViewMatrixHandle, 1, false, modelViewMatrix,
        0);
  }

  public int getVertexHandle() {
    return this.vertexHandle;
  }

  public void use() {
    GLES20.glUseProgram(this.program);
  }
}
